package com.lagou.test;

import com.lagou.dao.UserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class mybaits_test {

    @Test
    public void testInsert() {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory SqlSessionFactory = null;
        try {
            SqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = SqlSessionFactory.openSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = new User();
            user.setId(5);
            user.setUsername("xxxxxx");
            userDao.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testUpdate() {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory SqlSessionFactory = null;
        try {
            SqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = SqlSessionFactory.openSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = new User();
            user.setId(1);
            user.setUsername("路小雨");
            userDao.update(user);
            User byConditionAfter = userDao.findByCondition(user);
            System.out.println("byConditionAfter:" + byConditionAfter);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void testDelete() {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory SqlSessionFactory = null;
        try {
            SqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = SqlSessionFactory.openSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = new User();
            user.setId(1);
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory SqlSessionFactory = null;
        try {
            SqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = SqlSessionFactory.openSession();
            User user = new User();
            user.setId(1);
            user.setUsername("鲁迅");
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            List<User> all = userDao.findAll();
            System.out.println(all.toString());
            User byCondition = userDao.findByCondition(user);
            System.out.println(all.toString());
            System.out.println("byCondition:" + byCondition);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
